/*-
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * MantenimientoDeVehículos.java
 *
 * Created on 14-may-2012, 2:50:08
 */
package Interfaz_Grafica;

import Logica.LPersonas;
import Logica.LVehiculo;
import Objetos.Vehiculos;
import java.awt.Component;
import java.awt.Window;
import java.awt.event.WindowEvent;
import java.sql.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author Omar Heráclides
 */
public class MantenimientoDeVehículos extends javax.swing.JFrame {

    private Component parent;
    private Boolean modificable;
    private int Fila;
    private String codVehiculo;

    public String getCodVehiculo() {
        return codVehiculo;
    }

    public void setCodVehiculo(String codVehiculo) {
        this.codVehiculo = codVehiculo;
    }

    public int getFila() {
        return Fila;
    }

    public void setFila(int Fila) {
        this.Fila = Fila;
    }

    public Boolean getModificable() {
        return modificable;
    }

    public void setModificable(Boolean modificable) {
        this.modificable = modificable;
    }

    /** Creates new form MantenimientoDeVehículos */
    public MantenimientoDeVehículos() throws SQLException {
        this.setTitle("Mantenimiento de Vehículos");
        initComponents();
       
        CargarAnho();
        CargarEmpresaTransporte();
        CargarLineTransporte(this.jcbEmpresa.getSelectedItem().toString());
        CargarTipoVehiculo();
         setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        this.jcbEstadoSelected.setVisible(false);
        this.modificable = false;
    }

    private void CargarEmpresaTransporte() throws SQLException {
        LVehiculo objLVehiculo = new LVehiculo();
        ArrayList<String> arrl = new ArrayList<String>();
        arrl = objLVehiculo.ObtenerEmpresaTransporte();
        for (int i = 0; i < arrl.size(); i++) {
            this.jcbEmpresa.addItem(arrl.get(i).toString());
        }
    }

    private void CargarAnho() {
        Date annho = new Date();
        for (int i = 0; i < 25; i++) {
            //System.out.print(annho.getYear());
            this.jcbAnho.addItem(annho.getYear() - i + 1900);
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        label1 = new java.awt.Label();
        label2 = new java.awt.Label();
        txtPlaca = new java.awt.TextField();
        label5 = new java.awt.Label();
        jcbEmpresa = new javax.swing.JComboBox();
        jcbAnho = new javax.swing.JComboBox();
        jcbVehiculo = new javax.swing.JComboBox();
        label3 = new java.awt.Label();
        label4 = new java.awt.Label();
        jcbLinea = new javax.swing.JComboBox();
        jScrollPane3 = new javax.swing.JScrollPane();
        dtmTablaVehiculo = new javax.swing.JTable();
        btnEliminar = new javax.swing.JButton();
        btnCancelar = new javax.swing.JButton();
        button1 = new java.awt.Button();
        txtBuscar = new java.awt.TextField();
        btnAgregar = new javax.swing.JButton();
        btnModificar = new javax.swing.JButton();
        btnLimpiarTabla = new javax.swing.JButton();
        btnLimpiarTextBox = new javax.swing.JButton();
        jcbBuscar = new javax.swing.JComboBox();
        jcbEstadoSelected = new javax.swing.JComboBox();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        label1.setText("Linea de Transporte:");

        label2.setText("Placa de Vehículo:");

        label5.setText("Año:");

        jcbEmpresa.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                jcbEmpresaItemStateChanged(evt);
            }
        });
        jcbEmpresa.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jcbEmpresaActionPerformed(evt);
            }
        });

        label3.setText("Tipo de Vehiculo: ");

        label4.setText("Empresa de Transporte:");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(label5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(label2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(65, 65, 65)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jcbAnho, javax.swing.GroupLayout.PREFERRED_SIZE, 82, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(txtPlaca, javax.swing.GroupLayout.PREFERRED_SIZE, 106, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(label1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(label3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(label4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(32, 32, 32)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jcbEmpresa, 0, 123, Short.MAX_VALUE)
                            .addComponent(jcbLinea, 0, 123, Short.MAX_VALUE)
                            .addComponent(jcbVehiculo, 0, 123, Short.MAX_VALUE))))
                .addGap(71, 71, 71))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(label4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(label1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addComponent(jcbEmpresa, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jcbLinea, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(11, 11, 11)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(label3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jcbVehiculo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(label5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jcbAnho, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(26, 26, 26)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(label2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtPlaca, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(177, 177, 177))
        );

        label1.getAccessibleContext().setAccessibleName("lblLinea");
        txtPlaca.getAccessibleContext().setAccessibleName("txtPlacaVehiculo");

        dtmTablaVehiculo.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        dtmTablaVehiculo.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                dtmTablaVehiculoMouseClicked(evt);
            }
        });
        jScrollPane3.setViewportView(dtmTablaVehiculo);

        btnEliminar.setText("Eliminar");
        btnEliminar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEliminarActionPerformed(evt);
            }
        });

        btnCancelar.setText("Cancelar");
        btnCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelarActionPerformed(evt);
            }
        });

        button1.setLabel("Buscar :");
        button1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                button1ActionPerformed(evt);
            }
        });

        txtBuscar.setName("lblBuscar:"); // NOI18N

        btnAgregar.setText("Agregar");
        btnAgregar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAgregarActionPerformed(evt);
            }
        });

        btnModificar.setText("Modificar");
        btnModificar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnModificarActionPerformed(evt);
            }
        });

        btnLimpiarTabla.setText("Limpiar");
        btnLimpiarTabla.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnLimpiarTablaActionPerformed(evt);
            }
        });

        btnLimpiarTextBox.setText("Limpiar");
        btnLimpiarTextBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnLimpiarTextBoxActionPerformed(evt);
            }
        });

        jcbBuscar.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Placa", "Empresa", "Estado" }));
        jcbBuscar.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                jcbBuscarItemStateChanged(evt);
            }
        });
        jcbBuscar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jcbBuscarActionPerformed(evt);
            }
        });

        jcbEstadoSelected.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Activo", "Inactivo" }));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(53, 53, 53)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(button1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addGap(65, 65, 65)
                                        .addComponent(txtBuscar, javax.swing.GroupLayout.PREFERRED_SIZE, 98, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(jcbEstadoSelected, javax.swing.GroupLayout.PREFERRED_SIZE, 82, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                        .addComponent(jcbBuscar, javax.swing.GroupLayout.PREFERRED_SIZE, 82, javax.swing.GroupLayout.PREFERRED_SIZE))
                                    .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                            .addGroup(layout.createSequentialGroup()
                                .addGap(39, 39, 39)
                                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGap(42, 42, 42)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(btnLimpiarTextBox, javax.swing.GroupLayout.DEFAULT_SIZE, 75, Short.MAX_VALUE)
                            .addComponent(btnAgregar, javax.swing.GroupLayout.DEFAULT_SIZE, 75, Short.MAX_VALUE)
                            .addComponent(btnModificar, javax.swing.GroupLayout.DEFAULT_SIZE, 75, Short.MAX_VALUE)
                            .addComponent(btnLimpiarTabla, javax.swing.GroupLayout.DEFAULT_SIZE, 75, Short.MAX_VALUE)
                            .addComponent(btnEliminar, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 75, Short.MAX_VALUE)))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(541, 541, 541)
                        .addComponent(btnCancelar, javax.swing.GroupLayout.DEFAULT_SIZE, 81, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(54, 54, 54)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 209, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(37, 37, 37)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(button1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(txtBuscar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addGap(13, 13, 13)
                        .addComponent(btnAgregar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnModificar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnLimpiarTextBox)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 156, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jcbBuscar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jcbEstadoSelected, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addGap(23, 23, 23)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(btnEliminar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(btnLimpiarTabla))
                    .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 128, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 14, Short.MAX_VALUE)
                .addComponent(btnCancelar)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void btnAgregarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAgregarActionPerformed
// TODO add your handling code here:
    Boolean pageValid = true;
    String mensajeValid = this.txtPlaca.getText();
    pageValid = ValidarPagina(mensajeValid);

    if (pageValid == true) {

        Vehiculos objVehiculo = new Vehiculos();
        objVehiculo.setPlaca(this.txtPlaca.getText());
        objVehiculo.setAnhoFabricacion(this.jcbAnho.getSelectedItem().toString());
        try {
            objVehiculo.setIdEmpresaTransporte(ObtenerIdEmpresaTransporte(this.jcbEmpresa.getSelectedItem().toString()));
        } catch (SQLException ex) {
            Logger.getLogger(MantenimientoDeVehículos.class.getName()).log(Level.SEVERE, null, ex);
        }
        try {
            objVehiculo.setIdLineaDeTransporte(ObtenerIdLineaDeTransporte(this.jcbLinea.getSelectedItem().toString()));
        } catch (SQLException ex) {
            Logger.getLogger(MantenimientoDeVehículos.class.getName()).log(Level.SEVERE, null, ex);
        }
        try {
            objVehiculo.setIdTipoVehiculo(ObtenerIdTipoVehiculo(this.jcbVehiculo.getSelectedItem().toString()));
        } catch (SQLException ex) {
            Logger.getLogger(MantenimientoDeVehículos.class.getName()).log(Level.SEVERE, null, ex);
        }
        try {
            String numVar = ObtenerIdSecuencia();
            numVar = numVar.trim();
            System.out.println(numVar);
            int numInt = Integer.parseInt(numVar);
            objVehiculo.setIdVehiculo(String.valueOf(numInt + 1));
        } catch (SQLException ex) {
            Logger.getLogger(MantenimientoDeVehículos.class.getName()).log(Level.SEVERE, null, ex);
        }


        objVehiculo.setIdEstado("1");

        LVehiculo objLVehiculo = new LVehiculo();
        String mensaje = "Fallo";
        try {
            mensaje = objLVehiculo.AgregarVehiculo(objVehiculo);
        } catch (SQLException ex) {
            Logger.getLogger(MantenimientoDePersonas.class.getName()).log(Level.SEVERE, null, ex);
        }
        JOptionPane.showMessageDialog(null, mensaje,
                "Información", JOptionPane.INFORMATION_MESSAGE);
    } else {

        JOptionPane.showMessageDialog(null, "Debe ingresa un número entero en el campo placa.",
                "Información", JOptionPane.INFORMATION_MESSAGE);
    }
}//GEN-LAST:event_btnAgregarActionPerformed

private void button1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_button1ActionPerformed
// TODO add your handling code here:        
    LVehiculo objLVehiculo = new LVehiculo();
    if ((this.jcbEstadoSelected.isVisible() == false) && (this.txtBuscar.getText().equals("") == true)) {
        try {
            this.dtmTablaVehiculo.setModel(objLVehiculo.MostrarVehiculos());
        } catch (SQLException ex) {
            Logger.getLogger(MantenimientoDeVehículos.class.getName()).log(Level.SEVERE, null, ex);
        }
    } else {
        try {
            String estado;
            if (this.jcbEstadoSelected.getSelectedItem().equals("Activo") == true) {
                estado = "1                ";
            } else {
                estado = "0                ";
            }
            dtmTablaVehiculo.setModel(objLVehiculo.SelectVehiculo(this.txtBuscar.getText(), this.jcbBuscar.getSelectedItem().toString(), estado));
        } catch (SQLException ex) {
            Logger.getLogger(MantenimientoDeVehículos.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}//GEN-LAST:event_button1ActionPerformed

private void btnLimpiarTextBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnLimpiarTextBoxActionPerformed
// TODO add your handling code here:
    this.txtBuscar.setText("");
    this.txtPlaca.setText("");
    this.jcbEmpresa.setSelectedIndex(0);
    this.jcbLinea.setSelectedIndex(0);
    this.jcbVehiculo.setSelectedIndex(0);
    this.jcbAnho.setSelectedIndex(0);
}//GEN-LAST:event_btnLimpiarTextBoxActionPerformed

private void btnEliminarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEliminarActionPerformed
// TODO add your handling code here:
    int resp;
    String mensaje;
    LVehiculo objLVehiculo = new LVehiculo();

    if (modificable == true) {

        resp = JOptionPane.showConfirmDialog(null, "¿Desea Eliminar el registro?", "Pregunta", 0);
        if (resp == 0) {
            int row = this.getFila();
            if (row >= 0) {
                System.out.println("Row: " + row + " Cod= " + this.getCodVehiculo());
                try {
                    // JOptionPane.showMessageDialog(this, this.dtmTablaPersonas.getValueAt(row, 0));
                    //this.setCodVehiculo(Integer.parseInt((String) this.dtmTablaPersonas.getValueAt(row, 0)));
                    mensaje = objLVehiculo.EliminarVehiculo(this.getCodVehiculo());
                } catch (SQLException ex) {
                    Logger.getLogger(MantenimientoDeVehículos.class.getName()).log(Level.SEVERE, null, ex);
                }
                this.LimpiarTabla();
            }
        }
        this.setModificable(false);
    } else {

        JOptionPane.showMessageDialog(this, "¡Debe seleccionar una fila!");
    }
}//GEN-LAST:event_btnEliminarActionPerformed

private void btnLimpiarTablaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnLimpiarTablaActionPerformed
// TODO add your handling code here:
    LimpiarTabla();
    this.txtBuscar.setText("");
}//GEN-LAST:event_btnLimpiarTablaActionPerformed

private void jcbEmpresaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jcbEmpresaActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_jcbEmpresaActionPerformed
    public void windowClosing(WindowEvent we) {
        //tuJFRame.setResizable(false); 
    }

private void jcbEmpresaItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_jcbEmpresaItemStateChanged
    try {
        // TODO add your handling code here:
        CargarLineTransporte(this.jcbEmpresa.getSelectedItem().toString());
    } catch (SQLException ex) {
        Logger.getLogger(MantenimientoDeVehículos.class.getName()).log(Level.SEVERE, null, ex);
    }
}//GEN-LAST:event_jcbEmpresaItemStateChanged

private void btnCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelarActionPerformed
// TODO add your handling code here:
    this.dispose();
}//GEN-LAST:event_btnCancelarActionPerformed

private void btnModificarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnModificarActionPerformed
// TODO add your handling code here:
    int resp;
    Vehiculos objVehiculo = new Vehiculos();
    System.out.println("Modificable");
    if (modificable == true) {
        System.out.println("Modificable2");
        resp = JOptionPane.showConfirmDialog(null, "¿Desea Modificar el registro?", "Pregunta", 0);
        if (resp == 0) {
            try {
                LimpiarTabla();
                objVehiculo.setIdEmpresaTransporte(this.ObtenerIdEmpresaTransporte(this.jcbEmpresa.getSelectedItem().toString()));
                objVehiculo.setIdLineaDeTransporte(this.ObtenerIdLineaDeTransporte(this.jcbLinea.getSelectedItem().toString()));
                //  objVehiculo.setIdTipoVehiculo(this.jcbVehiculo.getSelectedItem().toString());
                objVehiculo.setAnhoFabricacion(this.jcbAnho.getSelectedItem().toString());
                objVehiculo.setPlaca(this.txtPlaca.getText());
                objVehiculo.setIdTipoVehiculo(this.ObtenerIdTipoVehiculo(this.jcbVehiculo.getSelectedItem().toString()));
                objVehiculo.setIdVehiculo(this.getCodVehiculo());
                this.limpiar();
                LVehiculo objLVehiculo = new LVehiculo();
                String mensaje = "Fallo";
                mensaje = objLVehiculo.ModificarVehiculo(objVehiculo);
                JOptionPane.showMessageDialog(null, mensaje,
                        "Información", JOptionPane.INFORMATION_MESSAGE);
            } catch (SQLException ex) {
                Logger.getLogger(MantenimientoDeVehículos.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        this.setModificable(false);
        this.limpiar();
    } else {
        JOptionPane.showMessageDialog(null, "Debe selecionar un registro de la tabla!", "Información", JOptionPane.INFORMATION_MESSAGE);
    }


}//GEN-LAST:event_btnModificarActionPerformed

private void dtmTablaVehiculoMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_dtmTablaVehiculoMouseClicked
// TODO add your handling code here:

    int row = this.dtmTablaVehiculo.rowAtPoint(evt.getPoint());
    if (row >= 0) {
        try {
            this.setModificable(true);
            this.setFila(row);
            //Date annho = new Date();
            int n = (2012 - Integer.parseInt(dtmTablaVehiculo.getModel().getValueAt(row, 5).toString()));
            System.out.println("ano=" + n);
            this.jcbAnho.setSelectedIndex(n);
            this.setCodVehiculo(dtmTablaVehiculo.getModel().getValueAt(row, 0).toString());
            // String titulos[] = {"Codigo", "Empresa de Transporte", "Linea de Transporte", "Placa", "Estado", "Año de Adquisición"};
            this.txtPlaca.setText(this.dtmTablaVehiculo.getModel().getValueAt(row, 3).toString());
            int idEmpresa;
            String idE = "";
            idE = this.ObtenerIdEmpresaTransporte(this.dtmTablaVehiculo.getModel().getValueAt(row, 1).toString());
            idEmpresa = Integer.parseInt(idE) - 1;
            this.jcbEmpresa.setSelectedIndex(idEmpresa);
            this.jcbLinea.setSelectedIndex(Integer.parseInt(this.ObtenerIdLineaDeTransporte(this.dtmTablaVehiculo.getModel().getValueAt(row, 2).toString())) - 1 - Integer.parseInt(ObtenerCantidadLineasEmpresas(idE)));
            System.out.println("IdTipo vehiculo: " + ObtenerIdTipoVehiculo(dtmTablaVehiculo.getModel().getValueAt(row, 6).toString().trim() + "bbbbbbbbbbbbbb"));
            this.jcbVehiculo.setSelectedIndex(Integer.parseInt(ObtenerIdTipoVehiculo(dtmTablaVehiculo.getModel().getValueAt(row, 6).toString().trim())) - 1);

        } catch (SQLException ex) {
            Logger.getLogger(MantenimientoDeVehículos.class.getName()).log(Level.SEVERE, null, ex);
        }
    } else {
        this.setModificable(false);
    }

}//GEN-LAST:event_dtmTablaVehiculoMouseClicked

private void jcbBuscarItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_jcbBuscarItemStateChanged
// TODO add your handling code here:
    if (this.jcbBuscar.getSelectedItem().equals("Estado")) {
        this.jcbEstadoSelected.setVisible(true);
        this.txtBuscar.setVisible(false);
    } else {
        this.jcbEstadoSelected.setVisible(false);
        this.txtBuscar.setVisible(true);
    }

}//GEN-LAST:event_jcbBuscarItemStateChanged

private void jcbBuscarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jcbBuscarActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_jcbBuscarActionPerformed
    /**
     * @param args the command line arguments
     */
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAgregar;
    private javax.swing.JButton btnCancelar;
    private javax.swing.JButton btnEliminar;
    private javax.swing.JButton btnLimpiarTabla;
    private javax.swing.JButton btnLimpiarTextBox;
    private javax.swing.JButton btnModificar;
    private java.awt.Button button1;
    private javax.swing.JTable dtmTablaVehiculo;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JComboBox jcbAnho;
    private javax.swing.JComboBox jcbBuscar;
    private javax.swing.JComboBox jcbEmpresa;
    private javax.swing.JComboBox jcbEstadoSelected;
    private javax.swing.JComboBox jcbLinea;
    private javax.swing.JComboBox jcbVehiculo;
    private java.awt.Label label1;
    private java.awt.Label label2;
    private java.awt.Label label3;
    private java.awt.Label label4;
    private java.awt.Label label5;
    private java.awt.TextField txtBuscar;
    private java.awt.TextField txtPlaca;
    // End of variables declaration//GEN-END:variables

    private void CargarLineTransporte(String nombre) throws SQLException {
        LVehiculo objLVehiculo = new LVehiculo();
        String id;
        id = objLVehiculo.ObtenerIdEmpresaTransporte(nombre);
        //JOptionPane.showMessageDialog(this,id.toString());
        ArrayList<String> arrLT = new ArrayList<String>();
        arrLT = objLVehiculo.ObtenerLineaDeTrasporte(id);
        this.jcbLinea.removeAllItems();
        for (int i = 0; i < arrLT.size(); i++) {
            this.jcbLinea.addItem(arrLT.get(i).toString());
        }
    }

    private void CargarTipoVehiculo() throws SQLException {
        LVehiculo objLVehiculo = new LVehiculo();
        String id;
        ArrayList<String> arrLT = new ArrayList<String>();
        arrLT = objLVehiculo.CargarTipoVehiculo();

        for (int i = 0; i < arrLT.size(); i++) {
            this.jcbVehiculo.addItem(arrLT.get(i).toString());
        }
    }

    private String ObtenerIdEmpresaTransporte(String nombre) throws SQLException {
        LVehiculo objLVehiculo = new LVehiculo();
        return objLVehiculo.ObtenerIdEmpresaTransporte(nombre);
    }

    private String ObtenerIdSecuencia() throws SQLException {
        LVehiculo objLVehiculo = new LVehiculo();
        return objLVehiculo.ObtenerIdSecuenciaVehiculo();
    }

    private String ObtenerIdLineaDeTransporte(String nombre) throws SQLException {
        LVehiculo objLVehiculo = new LVehiculo();
        return objLVehiculo.ObtenerIdLineaTransporte(nombre).trim();
    }

    private String ObtenerIdTipoVehiculo(String nombre) throws SQLException {
        LVehiculo objLVehiculo = new LVehiculo();
        return objLVehiculo.ObtenerIdTipoVehiculo(nombre);
    }

    private void LimpiarTabla() {
        LVehiculo objVehiculo = new LVehiculo();
        this.dtmTablaVehiculo.setModel(objVehiculo.LimpiarTabla());
    }

    private void limpiar() {
        this.txtPlaca.setText("");
    }

    private Boolean ValidarPagina(String cad) {

        for (int i = 0; i < cad.length(); i++) {
            if (!Character.isDigit(cad.charAt(i))) {
                return false;
            }
        }
        return true;
    }

    private String ObtenerCantidadLineasEmpresas(String idE) throws SQLException {
        LVehiculo objLVehiculo = new LVehiculo();
        return objLVehiculo.ObtenerCantidadLineasEmpresas(idE); 
    }
}
